﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Contents
{
    /// <summary>
    /// This object represents the methods of a Adv.
    /// </summary>
    internal class VideoDAO
    {

        #region general

        private static Video LoadObj(IDataReader datareader)
        {
            Video obj = new Video();
            obj.VideoID = MpDataHelp.GetInt(datareader, "VideoID");
            obj.CategoryID = MpDataHelp.GetInt(datareader, "CategoryID");
            obj.Title = MpDataHelp.GetString(datareader, "Title");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.Image = MpDataHelp.GetString(datareader, "Image");
            obj.LinkVideo = MpDataHelp.GetString(datareader, "LinkVideo");
            obj.Embed = MpDataHelp.GetString(datareader, "Embed");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            obj.ViewCount = MpDataHelp.GetInt(datareader, "ViewCount");
            obj.Height = MpDataHelp.GetString(datareader, "Height");
            obj.Width = MpDataHelp.GetString(datareader, "Width");
            obj.DateCreate = MpDataHelp.GetDateTime(datareader, "DateCreate");
            return obj;
        }

        #endregion

        public static int Video_Add(Video obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Video_Add");
            db.AddOutParameter(dbCommand, "VideoID", DbType.Int32, obj.VideoID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Image", DbType.String, obj.Image);
            db.AddInParameter(dbCommand, "LinkVideo", DbType.String, obj.LinkVideo);
            db.AddInParameter(dbCommand, "Embed", DbType.String, obj.Embed);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "ViewCount", DbType.Int32, obj.ViewCount);
            db.AddInParameter(dbCommand, "Height", DbType.String, obj.Height);
            db.AddInParameter(dbCommand, "Width", DbType.String, obj.Width);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@VideoID"));
            return -1;
        }
        public static int Video_Update(Video obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Video_Update");
            db.AddInParameter(dbCommand, "VideoID", DbType.Int32, obj.VideoID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Image", DbType.String, obj.Image);
            db.AddInParameter(dbCommand, "LinkVideo", DbType.String, obj.LinkVideo);
            db.AddInParameter(dbCommand, "Embed", DbType.String, obj.Embed);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "ViewCount", DbType.Int32, obj.ViewCount);
            db.AddInParameter(dbCommand, "Height", DbType.String, obj.Height);
            db.AddInParameter(dbCommand, "Width", DbType.String, obj.Width);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Video_Delete(int VideoID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Video_Delete");
            db.AddInParameter(dbCommand, "VideoID", DbType.Int32, VideoID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Video> Video_Select(int VideoID, int CategoryID, bool IsActive, int SortOrder)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Video_Select");
            db.AddInParameter(dbCommand, "VideoID", DbType.Int32, VideoID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, SortOrder);
            List<Video> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Video>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}

